package com.sino.struts2;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.sino.bean.TModule;
import com.sino.business.ITModuleService;
import com.sino.model.ModuleModel;
import com.sino.util.CreatQueryCondition;
import com.sino.util.PageControl;

public class TModuleAction extends ActionSupport {
	private ITModuleService tmoduleServiceImp;

	private String page = "1";// 当前页

	private String pageStr;// 分页字符串

	private List moduleList;// 模块列表

	private int pageRow = 0;

	private String moduleTitle; // 标题

	private int id; // 模块id

	private String name; // 模块名称

	private String sn; // 模块编号

	private String url; // 模块地址

	private Integer orderNum; // 模块排序号

	private int parentId;// 父模块id

	/**
	 * @param tmoduleServiceImp
	 *            the tmoduleServiceImp to set
	 */
	public void setTmoduleServiceImp(ITModuleService tmoduleServiceImp) {
		this.tmoduleServiceImp = tmoduleServiceImp;
	}

	/**
	 * @return the page
	 */
	public String getPage() {
		return page;
	}

	/**
	 * @param page
	 *            the page to set
	 */
	public void setPage(String page) {
		this.page = page;
	}

	/**
	 * @return the pageStr
	 */
	public String getPageStr() {
		return pageStr;
	}

	/**
	 * @param pageStr
	 *            the pageStr to set
	 */
	public void setPageStr(String pageStr) {
		this.pageStr = pageStr;
	}

	/**
	 * @return the moduleList
	 */
	public List getModuleList() {
		return moduleList;
	}

	/**
	 * @param moduleList
	 *            the moduleList to set
	 */
	public void setModuleList(List moduleList) {
		this.moduleList = moduleList;
	}

	/**
	 * @return the pageRow
	 */
	public int getPageRow() {
		return pageRow;
	}

	/**
	 * @param pageRow
	 *            the pageRow to set
	 */
	public void setPageRow(int pageRow) {
		this.pageRow = pageRow;
	}

	/**
	 * @return the moduleTitle
	 */
	public String getModuleTitle() {
		return moduleTitle;
	}

	/**
	 * @param moduleTitle
	 *            the moduleTitle to set
	 */
	public void setModuleTitle(String moduleTitle) {
		this.moduleTitle = moduleTitle;
	}

	/**
	 * @return the id
	 */
	public int getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(int id) {
		this.id = id;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the orderNum
	 */
	public Integer getOrderNum() {
		return orderNum;
	}

	/**
	 * @param orderNum
	 *            the orderNum to set
	 */
	public void setOrderNum(Integer orderNum) {
		this.orderNum = orderNum;
	}

	/**
	 * @return the sn
	 */
	public String getSn() {
		return sn;
	}

	/**
	 * @param sn
	 *            the sn to set
	 */
	public void setSn(String sn) {
		this.sn = sn;
	}

	/**
	 * @return the url
	 */
	public String getUrl() {
		return url;
	}

	/**
	 * @param url
	 *            the url to set
	 */
	public void setUrl(String url) {
		this.url = url;
	}

	/**
	 * @return the parentId
	 */
	public int getParentId() {
		return parentId;
	}

	/**
	 * @param parentId
	 *            the parentId to set
	 */
	public void setParentId(int parentId) {
		this.parentId = parentId;
	}

	/**
	 * 先构造一个ModuleModel，然后调用tmoduleServiceImp的saveTModule函数增加一个一级模块
	 * 
	 * @return
	 */
	public String saveParentTModule() {
		ModuleModel mm = new ModuleModel();
		mm.setName(name);
		mm.setOrderNum(orderNum);
		mm.setSn(sn);
		mm.setUrl(url);
		this.tmoduleServiceImp.saveTModule(mm);
		return SUCCESS;
	}

	/**
	 * 先构造一个ModuleModel，然后调用tmoduleServiceImp的updateModuleModel函数更新一个一级模块的详细信息
	 * 
	 * @return
	 */
	public String updateParentTModule() {
		ModuleModel mm = new ModuleModel();
		mm.setId(id);
		mm.setName(name);
		mm.setOrderNum(orderNum);
		mm.setSn(sn);
		mm.setUrl(url);
		this.tmoduleServiceImp.updateModuleModel(mm);
		return SUCCESS;
	}

	/**
	 * 通过分页的方式列出所有一级模块列表
	 * 
	 * @return
	 */
	public String listParentTModule() {
		PageControl pageControl = new PageControl();// 翻页控制类
		pageControl.setPage(page);// 设置当前页
		pageControl.setPageSize(10);// 设置每页显示的记录数量
		pageControl.setRowCount(this.tmoduleServiceImp
				.findModulePageCount("t.TModule is null"));// 设置总记录数
		int startRow = (pageControl.getPage() - 1) * pageControl.getPageSize();// 开始行数
		int pageSize = pageControl.getPageSize();// 每页显示的数量
		pageStr = pageControl.getPageInfo(
				"listParentTModule.action?method=parent", "");
		moduleList = this.tmoduleServiceImp.findModulePageList(startRow,
				pageSize, "t.TModule is null");// 分页查询模块信息
		moduleTitle = "模块列表";
		this.setPageStr(pageStr);
		this.setPageRow(startRow);
		return SUCCESS;
	}

	/**
	 * 调用tmoduleServiceImp的delParentTModule函数删除一个一级模块
	 * 
	 * @return
	 */
	public String delParentTModule() {
		boolean delPM = this.tmoduleServiceImp.delParentTModule(id);
		if (true == delPM) {
			return SUCCESS;
		}
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html; charset=utf-8");
		try {
			PrintWriter out = response.getWriter();
			out
					.println("<script language='JavaScript' type='text/JavaScript'>");
			out.println("alert('该模块已经被授权，禁止删除！');");
			out.println("location.href='listParentTModule.action';");
			out.println("</script>");
		} catch (IOException e) {
			throw new RuntimeException("删除模块异常！");
		}
		return null;
	}

	/**
	 * 通过分页的方式列出所有子菜单列表
	 * 
	 * @return
	 */
	public String listChidrenTModule() {
		HttpServletRequest request = ServletActionContext.getRequest();
		CreatQueryCondition creatQueryCondition = new CreatQueryCondition();// 条件查询类
		creatQueryCondition.initializtion(request);// 初始化查询条件
		PageControl pageControl = new PageControl();// 翻页控制类
		pageControl.setPage(page);// 设置当前页
		pageControl.setPageSize(10);// 设置每页显示的记录数量
		pageControl.setRowCount(this.tmoduleServiceImp
				.findModulePageCount("t.TModule.id=" + parentId));// 设置总记录数
		int startRow = (pageControl.getPage() - 1) * pageControl.getPageSize();// 开始行数
		int pageSize = pageControl.getPageSize();// 每页显示的数量
		pageStr = pageControl.getPageInfo(
				"listChidrenTModule.action?method=children",
				creatQueryCondition.getCondition());
		moduleList = this.tmoduleServiceImp.findModulePageList(startRow,
				pageSize, "t.TModule.id=" + parentId);// 分页查询模块信息
		moduleTitle = "子模块列表";
		this.setPageStr(pageStr);
		this.setPageRow(startRow);
		this.setParentId(parentId);
		return SUCCESS;
	}

	/**
	 * 先构造一个ModuleModel，然后调用tmoduleServiceImp的saveTModule函数保存一个子模块
	 * 保存完毕后在当前页面打开子模块的父模块的所有子模块列表
	 * 
	 * @return
	 */
	public String saveChildrenTmodule() {
		ModuleModel mm = new ModuleModel();
		mm.setName(name);
		mm.setOrderNum(orderNum);
		mm.setSn(sn);
		mm.setUrl(url);
		TModule pmm = new TModule();
		pmm.setId(parentId);
		mm.setTModule(pmm);
		this.tmoduleServiceImp.saveTModule(mm);
		// 用JavaScript的方式在当前页面重新载入父模块的所有子模块列表页面
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html; charset=utf-8");
		try {
			PrintWriter out = response.getWriter();
			out
					.println("<script language='JavaScript' type='text/JavaScript'>");
			// out.println("toSearch('save_div');");
			out.println("location.href='listChidrenTModule.action?parentId="
					+ parentId + "';");
			out.println("</script>");
		} catch (IOException e) {
			throw new RuntimeException("保存子模块异常！");
		}

		return null;
	}

	/**
	 * 先构造一个ModuleModel，然后调用tmoduleServiceImp的updateModuleModel函数更新一个子模块
	 * 更新完毕后在当前页面打开子模块的父模块的所有子模块列表
	 * 
	 * @return
	 */
	public String updateChildrenTModule() {
		ModuleModel mm = new ModuleModel();
		mm.setId(id);
		mm.setName(name);
		mm.setOrderNum(orderNum);
		mm.setSn(sn);
		mm.setUrl(url);
		this.tmoduleServiceImp.updateModuleModel(mm);
		// 用JavaScript的方式在当前页面重新载入父模块的所有子模块列表页面
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html; charset=utf-8");
		try {
			PrintWriter out = response.getWriter();
			out
					.println("<script language='JavaScript' type='text/JavaScript'>");
			// out.println("toSearch('save_div');");
			out.println("location.href='listChidrenTModule.action?parentId="
					+ parentId + "';");
			out.println("</script>");
		} catch (IOException e) {
			throw new RuntimeException("更新子模块异常！");
		}
		return null;
	}

	/**
	 * 通过模块id，调用tmoduleServiceImp的delChildrenTModule函数删除子模块
	 * 删除完毕后在当前页面打开子模块的父模块的所有子模块列表
	 * 
	 * @return
	 */
	public String delChildrenTModule() {
		boolean delCM = this.tmoduleServiceImp.delChildrenTModule(id);
		// 用JavaScript的方式在当前页面重新载入父模块的所有子模块列表页面
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html; charset=utf-8");
		try {
			PrintWriter out = response.getWriter();
			out
					.println("<script language='JavaScript' type='text/JavaScript'>");
			if (false == delCM) {
				out.println("alert('该模块已经被授权，禁止删除！');");
			}
			out.println("location.href='listChidrenTModule.action?parentId="
					+ parentId + "';");
			out.println("</script>");
		} catch (IOException e) {
			throw new RuntimeException("删除子模块异常！");
		}

		return null;
	}

}
